#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
    //printf("stdout:%d\n", stdout->_fileno);
    //printf("stdin:%d\n", stdin->_fileno);
    //printf("stderr:%d\n", stderr->_fileno);
    //printf("\n\n");

    umask(0);
    int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    //int fd = open("log.txt", O_RDONLY);
    if(fd < 0)
    {
        perror("open:");
        return 1;
    }
    dup2(fd, 1);
    printf("fd:%d\n", fd);
    int cnt = 5;
    while(cnt)
    {
        printf("hello world\n");
        --cnt;
    }
    

    //while(1)
    //{
    //    char buff[64];
    //    int n = read(fd, buff, sizeof(buff));
    //    if(n>0)
    //    {
    //        buff[n]=0;
    //        printf("%s", buff);
    //    }
    //    else if(n==0)
    //    {
    //        break;
    //    }
    //}
    //const char* msg = "hello world\n";
    //int a = 1234567;
    //int cnt = 4;
    //while(cnt)
    //{
    //    write(fd, msg, strlen(msg));
    //    char buff[16];
    //    memset(buff, 0, sizeof(buff));
    //    snprintf(buff, sizeof(buff), "%d\n", a);
    //    write(fd, buff, strlen(buff));
    //    --cnt;
    //}

    close(fd);
    return 0;
}
